import { reqCreateRoom, reqDeleteBookingInvalid, reqDeleteRoomByIds, reqGetRoomListByCondition, reqGetRoomLocationList, reqUpdateRoomById } from "@/api";
import { Notification,Message } from "element-ui";
export default({
    namespaced:true,
    actions:{
        async getRoomLocationList(context){
            let result = await reqGetRoomLocationList();
            if(result.code==='200'){
                context.commit('getRoomLocationList',result.other);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async getRoomListByCondition(context,value){
            let result = await reqGetRoomListByCondition(value);
            if(result.code==='200'){
                context.commit('getRoomListByCondition',result.other);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async createRoom(context,value){
            let result =await reqCreateRoom(value);
            if(result.code==='200'){
                context.commit('createRoom',value);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async updateRoomById(context,value){
            let result=await reqUpdateRoomById(value);
            if(result.code==='200'){
                context.commit('updateRoomById',value);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async deleteRoomByIds(context,value){
            Message({
                type: 'success',
                message: '正在删除...'
            });
            let result=await reqDeleteRoomByIds(value);
            if(result.code==='200'){
                context.commit('deleteRoomByIds',value);
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        },
        async deleteBookingInvalidRecord(context,value){
            let result=await reqDeleteBookingInvalid(value);
            if(result.code==='200'){
                context.commit('deleteBookingInvalidRecord')
            }else{
                Message({
                    message: '警告!'+'['+result.code+']'+'['+result.message+']',
                    type: 'warning'
                })
                return;
            }
        }
    },
    mutations:{
        getRoomLocationList(state,value){
            state.roomLocationList=value
        },
        getRoomListByCondition(state,value){
            state.roomList=value
        },
        createRoom(state,value){
            let obj=Object.assign(value,{'roomstate':1})
            state.roomList.push(obj)
        },
        // 为computed能够检测到列表变化，先将记录删除再插入
        updateRoomById(state,value){
            let obj=Object.assign(value,{'roomstate':1})
            state.roomList.forEach((element,index)=>{
                if(element.id===obj.id){
                    obj.number=parseInt(obj.number)
                    state.roomList.splice(index,1);
                    state.roomList.push(obj);
                }
            })
            // location.reload();
        },
        deleteRoomByIds(state,value){
            let delArr=value;
            let originArr=state.roomList;
            delArr.forEach((element,index)=> {
                originArr.forEach((e,i)=>{
                    if(element===e.id){
                        originArr.splice(i,1)
                    }
                })
            });            
        },
        deleteBookingInvalidRecord(){
            Notification({
                title: '通知',
                message: '删除无效预约记录成功',
                type: 'success'
            })
        }
    },
    state:{
        roomList:[],
        roomLocationList:[]
    },
    getters:{

    }
})
